package com.bawei.base.mvvm

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bawei.base.Res
import kotlinx.coroutines.launch

open class BaseViewModel:ViewModel() {
    fun<T> httpRequest(s:suspend ()->Res<T>,success:(T)->Unit){
        viewModelScope.launch {
            try {
                val res = s.invoke()
                if (res.code == 200){
                    success(res.data)
                }else{
                    Log.d( "BaseViewModel", "httpRequest: ${res.message}")
                }
            } catch (e: Exception) {
                Log.d("BaseViewModel", "httpRequest: ${e.message}")
            }
        }
    }
}